<!--
 * @Author: ZhaoZhiqi
 * @Date: 2024-09-03 13:44:04
 * @LastEditors: ZhaoZhiqi
 * @LastEditTime: 2025-01-03 18:40:33
 * @Description: App
 * @FilePath: \charging-mini\充电项目\App.vue
-->
<script>
import { loginAPI } from "./src/api/api";
import { getQueryString } from "./src/libs/utils";
const systemInfo = uni.getSystemInfoSync();
const accountInfo = uni.getAccountInfoSync();
console.log("systemInfo", systemInfo);
console.log("accountInfo", accountInfo);

const isWx = systemInfo.uniPlatform === "mp-weixin";
const isAli = systemInfo.uniPlatform === "mp-alipay";
const env = accountInfo.envVersion === "release" ? "prod" : "test";
const isProd = env === "prod";

export default {
    onLaunch: function (options) {
        console.log("App Launch", options);
        // 支付宝扫码跳转参数只能通过onLaunch获取，微信通过onLoad获取
        if (options.query && options.query.qrCode) {
            let url = options.query.qrCode;
            this.globalData.stationSerialNumPort = getQueryString(url, "stationSerialNumPort");
            this.globalData.port = getQueryString(url, "port");
        }
    },
    onShow: function () {
        console.log("App Show");
    },
    onHide: function () {
        console.log("App Hide");
    },
    globalData: {
        isWx,
        isAli,
        env,
        isProd,
        imgs: {
            search: "https://charging-mini.oss-cn-hangzhou.aliyuncs.com/charging-mini/test/user/1867463233509597185/1735892852790-GPFft4pKm6uo578c4a5314d2dcd8f50d09344e4deb91.png",
            close: "https://charging-mini.oss-cn-hangzhou.aliyuncs.com/charging-mini/test/user/1852264564548263937/1733135780650-yczL45mRNEF9df382b8ed04e594cc8172d8f66c46b71.png",
            invoice: "https://charging-mini.oss-cn-hangzhou.aliyuncs.com/charging-mini/test/user/1852264564548263937/1733132819276-wu53XvqcKAE37e1c0642c3b656405c4c16e63407f67c.png",
            mineLogout: "https://charging-mini.oss-cn-hangzhou.aliyuncs.com/charging-mini/test/user/1852264564548263937/1733131612825-afsfFosCw9AG605c7fd248cf8cb8d202f5e3b98b3973.png",
            mineOrder: "https://charging-mini.oss-cn-hangzhou.aliyuncs.com/charging-mini/test/user/1852264564548263937/1733131643892-tme7DfnHh2YA2d067834fc9b09083e3b703f5e026610.png",
            radioChecked: "https://charging-mini.oss-cn-hangzhou.aliyuncs.com/charging-mini/test/user/1852264564548263937/1733127860651-qImXOAjcDVB244b925a17d6e95b29b5bcf8d33800b72.png",
            radioUnchecked: "https://charging-mini.oss-cn-hangzhou.aliyuncs.com/charging-mini/test/user/1852264564548263937/1733127921553-1sckIghafSoe84ef767669596987faf613bf09c4165a.png",
            checked: "https://charging-mini.oss-cn-hangzhou.aliyuncs.com/charging-mini/test/user/1852264564548263937/1733122822654-ZpewwyRWHvePcb292292b371f4e10256add53ad1b3b3.png",
            unchecked: "https://charging-mini.oss-cn-hangzhou.aliyuncs.com/charging-mini/test/user/1852264564548263937/1733122855824-4oF0QzhYRjWO8e451b01dbc3b1587a49e27318f07e91.png",
            cardRecharge: "https://charging-mini.oss-cn-hangzhou.aliyuncs.com/charging-mini/test/user/1852264564548263937/1733059562962-KyoxO6ixnLxwb4a71d8a803cd5141730b21c2d036eb7.png",
            cardConsume: "https://charging-mini.oss-cn-hangzhou.aliyuncs.com/charging-mini/test/user/1852264564548263937/1733059617111-VmdbWJLu0UEB0be803fe24b45e7a14ece643dba02cc6.png",
            paySuccess: "https://charging-mini.oss-cn-hangzhou.aliyuncs.com/charging-mini/test/user/1852264564548263937/1730712081436-FEGRD0LSTiTq162179893087f915c3769e634b5474de.png",
            payFail: "https://charging-mini.oss-cn-hangzhou.aliyuncs.com/charging-mini/test/user/1852264564548263937/1730712028246-Ns6HMoSy498U4387bbd75d34e4de6acd6bfca549bd2b.png",
            logo: "https://charging-mini.oss-cn-hangzhou.aliyuncs.com/charging-mini/test/user/1843539391923097601/1729759143728-EZoY8Pq7syzY2f32a7be36fecd1c7972e7745dca00c2.png",
            plus: "https://charging-mini.oss-cn-hangzhou.aliyuncs.com/charging-mini/test/user/1843539391923097601/1729755429677-ek64nTL1Qslq97fc657edf2302d77c3e569177c1c94e.png",
            back: "https://charging-mini.oss-cn-hangzhou.aliyuncs.com/charging-mini/test/user/1843539391923097601/1729156631424-lTzKvnXxeTVRbdcd515335a165687e72716c621a8531.png",
            car: "https://charging-mini.oss-cn-hangzhou.aliyuncs.com/charging-mini/test/user/1843539391923097601/1729154171272-j2ZmoxiaRbf82875d9a0925def792000a56e3dac3a10.png",
            cardBg: "https://charging-mini.oss-cn-hangzhou.aliyuncs.com/charging-mini/test/user/1843539391923097601/1729146703953-SrkhBJI3b35U364f32eb5ea19f7cf9442203c109d94f.png",
            orderDetailBg: "https://charging-mini.oss-cn-hangzhou.aliyuncs.com/charging-mini/test/user/1843539391923097601/1729154305040-R83tgrcx7Pr2ef10bccb10fff2c5824ca8f3695df78e.png",
            stationIcon: "https://charging-mini.oss-cn-hangzhou.aliyuncs.com/charging-mini/test/user/1843539391923097601/1729154560083-mxUVZEpJQ07D6397140ca119dbe65f23764073cfa56e.png",
            copyIcon: "https://charging-mini.oss-cn-hangzhou.aliyuncs.com/charging-mini/test/user/1843539391923097601/1729154596186-ERNTAKQPs3fr8943d15c03b13c42b98977eabc910e76.png",
            orderTitle: "https://charging-mini.oss-cn-hangzhou.aliyuncs.com/charging-mini/test/user/1843539391923097601/1729761359815-EoyP7bDP33bO90c6f32a6f652054aa00805f377d1512.png",
            walletTitle: "https://charging-mini.oss-cn-hangzhou.aliyuncs.com/charging-mini/test/user/1843539391923097601/1729761313083-Fce72iSz0QNt40b5b478bc51b143292b5707e9472d1f.png",
        },

        // 支付宝中扫码选择充电口用到
        stationSerialNumPort: "",
        port: "",
    },
    methods: {
        // 登录
        login(notAutoLogin) {
            return new Promise((resolve, reject) => {
                uni.login({
                    success: (res) => {
                        console.log("login res", res);
                        loginAPI({
                            account_type: isWx ? 1 : 2, // wx 1 alipay 2
                            code: res.code,
                        }).then((res) => {
                            // 已绑定手机
                            if (!res.need_bind_phone && !notAutoLogin) {
                                uni.removeStorageSync("isLogout");
                                uni.setStorageSync("userInfo", res);
                            }
                            uni.setStorageSync("token", res.token);
                            resolve(res);
                        });
                    },
                });
            });
        },
        /**
         * @description: 开启通知
         * @param { Array } tmplIds 模板ids
         * @return {*}
         */
        showMessageToast(tmplIds, callBack) {
            console.log("tmplIds", tmplIds);
            if (isWx) {
                wx.requestSubscribeMessage({
                    tmplIds,
                    success(res) {
                        console.log("requestSubscribeMessage success", res);
                    },
                    fail(err) {
                        console.log("requestSubscribeMessage fail", err);
                        // 主开关未打开
                        if (err.errCode === 20004) {
                            uni.showModal({
                                title: "通知未开启",
                                content: "要去开启订阅消息接收通知吗?",
                                success: (res) => {
                                    if (res.confirm) {
                                        uni.openSetting();
                                    }
                                },
                            });
                        }
                    },
                    complete() {
                        if (callBack) {
                            console.warn("执行callBack");
                            callBack();
                        }
                    },
                });
            } else {
                my.requestSubscribeMessage({
                    entityIds: tmplIds,
                    success(res) {
                        console.log("requestSubscribeMessage success", res);
                    },
                    fail(err) {
                        console.log("requestSubscribeMessage fail", err);
                        // 主开关未打开
                        if (err.errCode === 20004) {
                            uni.showModal({
                                title: "通知未开启",
                                content: "要去开启订阅消息接收通知吗?",
                                success: (res) => {
                                    if (res.confirm) {
                                        uni.openSetting();
                                    }
                                },
                            });
                        }
                    },
                    complete() {
                        if (callBack) {
                            console.warn("执行callBack");
                            callBack();
                        }
                    },
                });
            }
        },
    },
};
</script>

<style>
/*每个页面公共css */
</style>
